package com.rock.code.bigdata.bigdatainterface.service;

import com.rock.code.bigdata.bigdatainterface.util.MyUtil;
import com.rock.code.hdfs.HdfsAPP;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.rock.code.bigdata.bigdatainterface.util.MyUtil.timeToTimeStamp;


public class CollIfOrNotTask implements Job {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HistoryDataService historyDataService = (HistoryDataService) jobExecutionContext.getMergedJobDataMap().get("historyDataService");
        if (historyDataService.isOrNotEnd()) {
            //执行文件上传
            String fileClass = "collHisdata";
            Date today = new Date();
            String date = sdf.format(today);
            String day = date.split(" ")[0] + " " + "00:00:00";
            String taskEnd = timeToTimeStamp(day);
            String taskStart = Long.parseLong(taskEnd) - (24 * 60 * 60 * 1000) + "";
            String filedate = MyUtil.stampToDate(taskStart);
            String year = filedate.split(" ")[0].split("-")[0];
            String timeDay = filedate.split(" ")[0].replaceAll("-", "");
            try {
                MyUtil.putFileToHdfs(fileClass, year, timeDay);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //在hdfs上创建标志文件监视数据是否上传完成
            String hdfsPath = "/user/xxjsb_zk/flag/performance/bnms/original/" + timeDay + "/";

            try {
                new HdfsAPP().mkdirs(hdfsPath + "_SUCCESS");
            } catch (Exception e) {
                e.printStackTrace();
            }
//            //执行成功，删除文件
//            String filename = "hisData" + timeDay + ".txt";
//
//            MyUtil.delFile("/root/datas/", filename);
//            System.out.println(filename + "文件已删除！");
        }


    }


}
